/*
Copyright 2010  Murali Devi (profdevi@gmail.com)

This file is part of ComineGL Project.

ComineGL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ComineGL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ComineGL.  If not, see <http://www.gnu.org/licenses/>.

This code uses the irrlicht and irrklang libraries.  Please read
the libraries copyrights at 

	irrlicht library:  http://irrlicht.sourceforge.net
	irrklang library:  http://www.ambiera.com/irrklang

*/



//v0.14 copyright Comine.com 20101212U01
#include <windows.h>
#include "MStdLib.h"
#include "Global.h"
#include "ComineGLSound.h"



/////////////////////////////////////////////
MDLLFUN bool CGLSoundLoad(int soundid,const char *soundfile)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	// First Remove existing soundid
	if(GlobalSoundList.IsKey(soundid)==true)
		{
		GlobalSoundList.Remove(soundid);
		MStdPrintf("Removing Existing Sound with id %d\n",soundid);
		}

	TRefCount<MCGLSoundInfo> *newsoundref=new TRefCount<MCGLSoundInfo>;
	if(newsoundref==NULL)
		{
		MStdPrintf("**Unable to alloc memory\n");
		return false;
		}

	if(newsoundref->Create(GlobalRefSoundDevice,soundfile)==false)
		{
		MStdPrintf("**Unable to load sound %s\n",soundfile);
		newsoundref->Drop();
		return false;
		}

	if(GlobalSoundList.Add(soundid,newsoundref)==false)
		{
		newsoundref->Drop();
		return false;
		}
	
	newsoundref->Drop();
	return true;
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLSoundExists(int soundid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}
	
	return GlobalSoundList.IsKey(soundid);	
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLSoundUnload(int soundid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalSoundList.IsKey(soundid)==false)
		{
		MStdPrintf("**No sound found for id %d\n",soundid);
		return false;
		}

	if(GlobalSoundList.Remove(soundid)==false)
		{
		MStdPrintf("**Unable to remove sound id %d\n",soundid);
		return false;
		}
	
	return true;
	}


/////////////////////////////////////////////////
MDLLFUN bool CGLSoundUnloadAll(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	if(GlobalSoundList.Clear()==false)
		{
		return false;
		}

	return true;
	}


//////////////////////////////////////////////////
MDLLFUN int CGLSoundGetCount(void)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return 0;
		}
	
	return GlobalSoundList.GetCount();
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLSoundPlay(int soundid,bool loop)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLSoundInfo *soundinfo=GlobalSoundList.Find(soundid);
	if(soundinfo==NULL)
		{
		return false;
		}

	if(soundinfo->Play(loop)==false)
		{
		return false;
		}

	return true;
	}


///////////////////////////////////////////////////
MDLLFUN bool CGLSoundPlay3D(int soundid,float x,float y,float z,bool loop)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLSoundInfo *soundinfo=GlobalSoundList.Find(soundid);
	if(soundinfo==NULL)
		{
		return false;
		}

	if(soundinfo->Play3D(x,y,z,loop)==false)
		{
		return false;
		}

	return true;	
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLSoundSetListener(float x,float y,float z
				,float velx,float vely,float velz
				,float nx,float ny,float nz)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}
	
	GlobalRefSoundDevice->setListenerPosition(vec3df(x,y,z)
			,vec3df(nx,ny,nz)
			,vec3df(velx,vely,velz)
			,vec3df(0,1,0) );

	return true;
	}


//////////////////////////////////////////////////
MDLLFUN bool CGLSoundStop(int soundid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLSoundInfo *soundinfo=GlobalSoundList.Find(soundid);
	if(soundinfo==NULL)
		{
		return false;
		}

	if(soundinfo->Stop()==false)
		{
		return false;
		}

	return true;		
	}


/////////////////////////////////////////////////////////
MDLLFUN bool CGLSoundContinue(int soundid)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLSoundInfo *soundinfo=GlobalSoundList.Find(soundid);
	if(soundinfo==NULL)
		{
		return false;
		}

	if(soundinfo->Stop()==false)
		{
		return false;
		}

	return true;	
	}


/////////////////////////////////////////////////////
MDLLFUN bool CGLSoundSetVolume(int soundid,float vol)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLSoundInfo *soundinfo=GlobalSoundList.Find(soundid);
	if(soundinfo==NULL)
		{
		return false;
		}

	if(soundinfo->SetVolume(vol)==false)
		{
		return false;
		}
	
	return true;
	}


/////////////////////////////////////////////////////
MDLLFUN bool CGLSoundSetPan(int soundid,float pan)
	{
	if(GlobalInitialized==false)
		{
		MStdPrintf("**Engine is not initialized\n");
		return false;
		}

	MCGLSoundInfo *soundinfo=GlobalSoundList.Find(soundid);
	if(soundinfo==NULL)
		{
		return false;
		}

	if(soundinfo->SetPan(pan)==false)
		{
		return false;
		}

	return true;	
	}


